﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALModificationPropertySelectValue : DALBase
    {
        public static DALModificationPropertySelectValue Singleton = new DALModificationPropertySelectValue();

        public ModificationPropertySelectValue Add(ModificationPropertySelectValue gtBrand)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var newBrand = new ModificationPropertySelectValue();
                newBrand.Id = gtBrand.Id;
                newBrand.Value = gtBrand.Value;

                db.ModificationPropertySelectValues.InsertOnSubmit(newBrand);
                db.SubmitChanges();
                gtBrand.Id = newBrand.Id;
                return gtBrand;
            }
        }

        public List<GTModificationPropertySelectValue> GetByPropertyId(int propertyId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var brds =
                 from p in db.ModificationPropertySelectValues
                 where p.Id == propertyId
                 select p;


                var brands = brds.ToList();



                var ret = new List<GTModificationPropertySelectValue>();
                foreach (var brand in brands)
                {
                    GTModificationPropertySelectValue gtBrand = new GTModificationPropertySelectValue();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var list = db.ModificationPropertySelectValues.Where(x => x.Id == id).ToList();
                foreach (var modificationPropertySelectValue in list)
                {
                    db.ModificationPropertySelectValues.DeleteOnSubmit(modificationPropertySelectValue);
                    db.SubmitChanges();
                }
            }
        }





    }
}
